package sh.grapendaal.tsuushin.module;

import java.util.HashSet;
import java.util.Set;

import javax.inject.Inject;

import sh.grapendaal.tsuushin.core.RunnableCommand;
import sh.grapendaal.tsuushin.service.CommandService;

/**
 * The Command Module Service Proxy will keep track of what commands the module
 * this proxy is attached to has registered. This will prevent the module from
 * unregistering commands which belong to other modules or even the host
 * application.
 */
public class CommandModuleServiceProxy implements CommandService {
	@Inject
	private CommandService commandService;

	private Set<String> triggers = new HashSet<String>();

	@Override
	public void setPrefix(char prefix) {
		throw new IllegalArgumentException("Modules are not allowed to change the prefix.");
	}

	@Override
	public char getPrefix() {
		return this.commandService.getPrefix();
	}

	@Override
	public void registerCommand(String trigger, String permission, RunnableCommand callback) {
		this.commandService.registerCommand(trigger, permission, callback);
		this.triggers.add(trigger);
	}

	@Override
	public void unregisterCommand(String trigger) {
		if (!commandExists(trigger)) {
			throw new IllegalArgumentException("A command with trigger \"" + trigger + "\" does not exist.");
		}
		if (!this.triggers.contains(trigger)) {
			throw new IllegalArgumentException("Command \"" + trigger + "\" is not registered by this module.");
		}
		this.commandService.unregisterCommand(trigger);
	}

	@Override
	public boolean commandExists(String trigger) {
		return this.commandService.commandExists(trigger);
	}

}
